package com.sakura.hk.sakura.springboot.plugin.task;

import com.sakura.hk.sakura.springboot.plugin.SakuraExtension;
import org.gradle.api.DefaultTask;
import org.gradle.api.Project;
import org.gradle.api.file.FileTree;
import org.gradle.api.internal.AbstractTask;
import org.gradle.api.tasks.TaskAction;

import java.io.*;
import java.nio.channels.FileChannel;

/**
 * 更改原生Springboot的启动文件中声明的启动类
 *
 * @Author: 贺坤
 * @Date: 2021/7/13 16:21
 */
public class SetSpringbootStartupMainClass extends DefaultTask {

    /**
     * 当前任务名称
     */
    public static final String MY_TASK_NAME = "setSpringbootStartupMainClass";
    /**
     * 插件启动类 类名
     */
    private String myLauncherName;
    /**
     * 包名
     */
    private String myLauncherPackageName;
    /**
     * 缓存目录
     */
    private File cacheDir;
    /**
     * 缓存目录名称
     */
    private String cacheName;
    /**
     * 换行
     */
    private static final String LINE_FEED = "\r\n";
    @TaskAction
    public void action() {
        // 初始化
        init();
        // 检查缓存目录是否存在
        File cacheFile = new File(cacheDir, cacheName);
        cacheFile = new File(new File(cacheFile, "META-INF"), "MANIFEST.MF");
        try {
            FileInputStream in = new FileInputStream(cacheFile);
            InputStreamReader read = new InputStreamReader(in);
            BufferedReader br = new BufferedReader(read);
            StringBuilder stringBuilder = new StringBuilder();
            String path;
            while ((path = br.readLine()) != null) {
                if (path.startsWith("Main-Class:")) {
                    stringBuilder.append("Main-Class: ");
                    stringBuilder.append(myLauncherPackageName);
                    stringBuilder.append(".");
                    stringBuilder.append(myLauncherName);
                    stringBuilder.append(LINE_FEED);
                } else {
                    stringBuilder.append(path).append(LINE_FEED);
                }
            }
            br.close();
            read.close();
            in.close();
            FileWriter writer = new FileWriter(cacheFile);
            BufferedWriter out = new BufferedWriter(writer);
            out.write(stringBuilder.toString());
            out.close();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    public static AbstractTask foundMyTask(Project project) {
        SetSpringbootStartupMainClass task = project.getTasks().create(MY_TASK_NAME, SetSpringbootStartupMainClass.class);
        task.dependsOn(GenerateMyMainClassFile.MY_TASK_NAME);
        task.setDescription("更改原生Springboot的启动文件中声明的启动类");
        return task;
    }

    /**
     * 初始化相关变量
     */
    private void init() {
        SakuraExtension sakuraExtension = getProject().getExtensions().getByType(SakuraExtension.class);
        this.myLauncherName = sakuraExtension.getMyLauncherName();
        this.myLauncherPackageName = sakuraExtension.getMyLauncherPackageName();
        this.cacheDir = sakuraExtension.getCacheDir();
        this.cacheName = sakuraExtension.getCacheName();
    }
}
